/**
 * 
 */
package com.surelution.lansao.wxmp

import com.surelution.lansao.Member;
import com.surelution.lansao.Partner;
import com.surelution.lansao.PartnerMember;
import com.surelution.whistle.core.Attribute;
import com.surelution.whistle.core.BaseAction

/**
 * @author <a href="mailto:guangzong.syu@gmail.com">Guangzong</a>
 *
 */
class SubscribeAction extends BaseAction {

	/* (non-Javadoc)
	 * @see com.surelution.whistle.core.BaseAction#accept()
	 */
	@Override
	public boolean accept() {
		("Hello2BizUser" == getParam(Attribute.KEY_Content)) || ("event" == getParam(Attribute.KEY_MsgType) && "subscribe" == getParam("Event"))
	}

	/* (non-Javadoc)
	 * @see com.surelution.whistle.core.BaseAction#execute()
	 */
	@Override
	public void execute() {
		def now = new Date()
		def partner = Partner.findByWxOpenId(getParam(Attribute.KEY_ToUserName))
		def openId = getParam(Attribute.KEY_FromUserName)
		def mb = Member.findByOpenId(openId)
		if(!mb) {
			mb = new Member(openId:openId, registerDate:now)
			mb.save()
		}
		def pm = PartnerMember.findByPartnerAndMember(partner, mb)
		if(!pm) {
			pm = new PartnerMember(partner:partner, member:mb, fellowDate:now, active:true)
			pm.save(flush:true)
		}
		String slId = IdRefuser.getRefusedId(mb.id)
		put(new Attribute(Attribute.KEY_Content, "感谢关注蓝嫂午餐直送[${slId}]"))
	}

}
